layui.use(['layer', 'form', 'admin', 'ax','upload'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var layer = layui.layer;
    var upload = layui.upload;
    /**
     * 检索摘要
     */
    var zyAjax = new $ax(Feng.ctxPath + "/btyBxd/zyList");
    var zyResult = zyAjax.start();
    $.each(zyResult, function( index, val ) {
        $("#zy").append("<option value='"+val.id+"'>"+val.name+"</option>");
    } );
    form.render('select');
    var ajax = new $ax(Feng.ctxPath + "/btyBxd/info?id=" + $("#id").val());
    var result = ajax.start();
    var roleFlag=result.roleFlag;
    if(roleFlag==1){
        $("#flag").attr("disabled","disabled");
    }
    if(result !=null){
        form.val('noticeForm', result);
        if(result.picture !=null && result.picture !=""){
            getPicture("layer-photos-demo",result.picture);
        }else{
            $("#layer-photos-demo").html("");
        }

    }

    var flag=result.flag;
    if(flag==1){
        $("#btyBxdaId").attr("disabled","disabled");
        $("#zcje").attr("readonly","readonly");
        $("#zy").attr("readonly","readonly");
        $("#bz").attr("readonly","readonly");
        //普通图片上传禁用
        form.render('select');
    }


    //图片回填
    function getPicture(id,picture){
        $("#"+id).html(
            "<div class='gs-upload-file-container'>"+
            "<div class='gs-upload-ele'>"+
            "<div class='progress-bar progress-bar-success progress-bar-striped gs-upload-progress-bar' style='width: 188px; display: none;'></div>" +
            "<div class='gs-upload-ele-img-div'>"+
            "<img class='gs-upload-ele-img' src='"+picture+"' style='cursor: pointer;width: 150px;height: 150px;' id='gs-upload-ele-img'>	"+
            "</div>	"+
            "<div class='gs-upload-ele-content'>"+
            "<div class='gs-upload-ele-title-div'></div>"+
            "<div class='gs-upload-ele-menu'>"+
            "</div></div></div></div>"
        );
    }
    layer.photos({
        photos: '#layer-photos-demo',
        anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
    });

    layer.photos({
        photos: '#layer-photos-demo-back',
        anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
    });


    //普通图片上传
    var uploadInst = upload.render({
        elem: '#upload_btn',
        url: Feng.ctxPath + '/btyBxd/upload',
        before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $("#name").val(file.name);
                //拼接附件样式
                $("#layer-photos-demo").html(
                    "<div class='gs-upload-file-container'>"+
                    "<div class='gs-upload-ele'>"+
                    "<div class='progress-bar progress-bar-success progress-bar-striped gs-upload-progress-bar' style='width: 188px; display: none;'></div>" +
                    "<div class='gs-upload-ele-img-div'>"+
                    "<img class='gs-upload-ele-img' src='' style='cursor: pointer;width: 150px;height: 150px;' id='gs-upload-ele-img'>	"+
                    "</div>	"+
                    "<div class='gs-upload-ele-content'>"+
                    "<div class='gs-upload-ele-title-div'></div>"+
                    "<div class='gs-upload-ele-menu'>"+
                    "</div></div></div></div>"
                );

                //回填图片src
                $("#gs-upload-ele-img").attr("src",result);
                //图片预览
                layer.photos({
                    photos: '#layer-photos-demo',
                    anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
                });
            });
        },
        done: function(res){
            //如果上传失败
            if(res.code === 1){
                return layer.msg(i18n("system.upload_fail"));
            }
            //上传成功
            $("#picture").val(res.data.src);
            layer.msg("上传成功");
        },
        error: function(){
            //演示失败状态，并实现重传
            var msgText = $('#msgText');
            msgText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs upload-reload">重试</a>');
            msgText.find('.upload-reload').on('click', function(){
                uploadInst.upload();
            });
        }
    });





    form.on('select(flag)', function(data){
        var flag=data.value;
        if(flag==1){
            $("#btyBxdaId").attr("disabled","disabled");
            $("#zcje").attr("readonly","readonly");
            $("#zy").attr("readonly","readonly");
            $("#bz").attr("readonly","readonly");
            form.render('select');
            //普通图片上传禁用
            $('#upload_btn').addClass("layui-btn-disabled").attr("disabled",true);
        }else if(flag==0){
            $('#btyBxdaId').removeAttr("disabled");
            $("#zcje").removeAttr("readonly");
            $("#zy").removeAttr("readonly");
            $("#bz").removeAttr("readonly");
            //普通图片上传
            $('#upload_btn').removeClass("layui-btn-disabled").attr("disabled",false);
        }
        form.render('select');

    });

    // 表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        $('#btyBxdaId').removeAttr("disabled");
        var zyName=$("#zy").find("option:selected").text();
        var ajax = new $ax(Feng.ctxPath + "/btyBxd/updateZc", function (data) {
            Feng.success("修改成功！");
            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);
            //关掉对话框
            admin.closeThisDialog();

        }, function (data) {
            Feng.error("修改失败！" + data.responseJSON.message)
        });
        data.field.zy=zyName;
        ajax.set(data.field);
        ajax.start();
        //添加 return false 可成功跳转页面
        return false;
    });

});